package com.gp.gback.system.common.mapping_support;

import org.jboss.logging.MDC;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by admin on 16/11/17.
 */
@WebFilter(urlPatterns = "/*")
@Component
public class DataTransferFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        MDC.put("ip", servletRequest.getRemoteAddr());
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if ("POST".equals(request.getMethod().toUpperCase()) && "application/json".equalsIgnoreCase(request.getContentType())) {
            filterChain.doFilter(new HttpServletRequestWrapper4JsonToForm(request), servletResponse);
            return;
        }
        //放行，request不做处理
        filterChain.doFilter(request, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
